﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebSite_CodeMan.Sys_CodeMan_Manage
{
    public partial class Admin_Update_Note : System.Web.UI.Page
    {
        protected string ProImage = "../UpLoad/Images/NoImage.gif";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BaseClass.BaseClass.GetNoteClassMes(DDLProClass);
                LoadData();    
            }
        }

        private void LoadData()
        {
            string strNID = Common.GetMes.GetRequestQuery(Request,"NID");
            if (!string.IsNullOrEmpty(strNID))
            {
                OleDbDataReader DataRead = BLL.NoteInfo.GetAllNoteInfo(strNID);
                if (DataRead.HasRows)
                {
                    DataRead.Read();
                    DDLProClass.SelectedValue = DataRead["T_NoteClassID"].ToString();
                    txtProName.Value = DataRead["T_NoteTitle"].ToString();
                    txtKey.Value = DataRead["T_NoteKey"].ToString();
                    txtTime.Value = DataRead["T_DateTime"].ToString();
                    txtDescription.Value = DataRead["T_NoteDes"].ToString();
                    txtNote.Value = Server.HtmlDecode(DataRead["T_NoteMes"].ToString());
                    RBProTop.Checked = (DataRead["T_NoteFlag"].ToString() == "1") ? true : false;
                    HImageName.Value = ((string.IsNullOrEmpty(DataRead["T_NoteImage"].ToString())) ? "NoImage.gif" : DataRead["T_NoteImage"].ToString());
                    ProImage = "../UpLoad/Images/Small/" + HImageName.Value;
                }
                DataRead.Close();
                DataRead.Dispose();
            }
        }
        

        protected void BtnUpLoad_Click(object sender, EventArgs e)
        {
            HImageName.Value = (HImageName.Value == "noImage.gif") ? Common.GetMes.GetNowTimeString() + UploadImg.FileName.Substring(UploadImg.FileName.LastIndexOf(".")) : HImageName.Value;
            switch (BaseClass.BaseClass.UpFile(Page, UploadImg, "img", HImageName.Value))
            {
                case 1:
                    string newsImageName = HImageName.Value;
                    string smallImageName = HImageName.Value.Substring(0, HImageName.Value.IndexOf("."));
                    BaseClass.BaseClass.MakeThumbnail(Server.MapPath("~/UpLoad/Images/" + newsImageName), Server.MapPath("~/UpLoad/Images/Small/" + smallImageName + ".jpg"), 50, 60, "HW","jpg");
                    ProImage = "../UpLoad/Images/Small/" + newsImageName;
                    break;
                case -1:
                    Common.MessageBox.Show(Page, "e1", "上传文件格式有误，限制使用gif，jpeg。");
                    break;
                case -2:
                    Common.MessageBox.Show(Page, "e2", "上传文件太大，请确保图片小于500K。");
                    break;
                default:
                    Common.MessageBox.Show(Page, "e2", "未知错误，请重试或联系管理人员。");
                    break;
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string strPID = Common.GetMes.GetRequestQuery(Request, "NID");
            if (BLL.NoteInfo.EditNoteMes(strPID, Server.HtmlEncode(txtProName.Value), DDLProClass.SelectedValue, Server.HtmlEncode(txtDescription.Value), Server.HtmlEncode(txtKey.Value), ((string.IsNullOrEmpty(txtTime.Value.Trim())) ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : txtTime.Value.Trim()), Server.HtmlEncode(txtNote.Value), ((RBProTop.Checked) ? "1" : "0"), HImageName.Value, ""))
            {
                if (string.IsNullOrEmpty(strPID))
                {
                    Common.MessageBox.myScriptMes(Page, "if(confirm(\"信息保存成功，是否继续添加？否则返回信息列表。\")) {location.href=\"Admin_Update_Note.aspx\"} else  {location.href=\"Admin_Note_Mes.aspx\"}", "suess");

                }
                else
                {
                    Common.MessageBox.Show(Page, "信息保存成功......", "Admin_Note_Mes.aspx", "error");
                }
            }
            else
            {
                Common.MessageBox.Show(Page,"信息保存失败，请重试或联系技术人员......","error");
            }

        }
    }
}
